package uriel.frankel;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.preference.PreferenceManager;
import java.text.DecimalFormat;
import uriel.frankel.R;
import uriel.frankel.model.Algorithem;






public class MainActivity extends Activity implements OnClickListener {

	Algorithem irsMe;
	SharedPreferences settings;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		View newButton = findViewById(R.id.buttonGoResult);
		newButton.setOnClickListener(this);
		View newButton2 = findViewById(R.id.preferences);
		newButton2.setOnClickListener(this);
		View newButton3 = findViewById(R.id.about_button);
		newButton3.setOnClickListener(this);
		Button salaryPoll = (Button) findViewById(R.id.salary_poll);
		salaryPoll.setOnClickListener(this);
		
		irsMe = Algorithem.GetInstance();
		
		settings = PreferenceManager.getDefaultSharedPreferences(this);
		PackageInfo pInfo = null;
		try {
			pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		int version = 0;
		if(pInfo!=null){
		 version = pInfo.versionCode;
		}
		
		if(settings.getInt("VERSION_CODE", 13)<version){
			settings.edit().
			putInt("VERSION_CODE", version).
			commit();
			Intent intent = new Intent(this,WhatsNewDialog.class);
			settings.edit().putBoolean("FIRST_TIME", false).commit();
			startActivity(intent);
		}
		
		
	}

	public void onClick(View v) {
		EditText SalaryLabel = (EditText) findViewById(R.id.editText1);
		switch (v.getId()) {

		case R.id.preferences:
			// SalaryLabel.setText("");
			Intent preferences_intent = new Intent(MainActivity.this,
					Preferences.class);
			MainActivity.this.startActivity(preferences_intent);

			return;

		case R.id.buttonGoResult:
			try {
				String pointsAsString = settings.getString("points", "2.25");
				irsMe.points = Double.parseDouble(pointsAsString);

				String pensionAsString = settings.getString("pension_percent",
						"5");
				irsMe.setPensionPercentage(Double.parseDouble(pensionAsString) * 0.01);

				String drivingString = settings.getString("driving_refund",
						"0");
				irsMe.driving = Double.parseDouble(drivingString);

				boolean isKeren = settings.getBoolean(
						"keren", true);
				irsMe.enableTrainingFund(isKeren);
				String salaryWithoutPensionAsString = settings.getString(
						"no_social_edditions_to_salary", "0");
				irsMe.setSalaryWithoutPension( Double
						.parseDouble(salaryWithoutPensionAsString));

				String benefits = settings.getString(
						"no_social_edditions", "0");
				irsMe.setBenefits( Double
						.parseDouble(benefits));


				boolean isLeasing = settings.getBoolean("leasing", false);
				irsMe.setLeasing(isLeasing);

				if (isLeasing) {
					boolean hybrid = settings.getBoolean("hybrid", false);
					irsMe.setHybrid(hybrid);

					String paymentToCompanyAsString = settings.getString(
							"payment_for_company", "0.0");
					double paymentToCompany = Double
							.parseDouble(paymentToCompanyAsString);
					irsMe.setMonthlyPaymentForCar(paymentToCompany);

					String carsPriceAsString = (String) settings.getString(
							"car_price", "0.0");
					double carsPrice = Double.parseDouble(carsPriceAsString);
					irsMe.setCarPrice(carsPrice);
				}
			} catch (Exception e) {
				Toast.makeText(this,
						"יש שדות ריקים דפוקים בהגדרות. לך לשם ותסדר אותם",
						Toast.LENGTH_LONG).show();
				return;
			}

			if (SalaryLabel.length() == 0)
				return;
			String tempString = SalaryLabel.getText().toString();
			tempString = tempString.replace(",", "");
			int salaryAsInteger = 0;
			try {
				salaryAsInteger = Integer.parseInt(tempString);
			} catch (NumberFormatException nfe) {
				SalaryLabel.getText().clear();
				return;
			}
			irsMe = Algorithem.GetInstance();
			irsMe.setBaseSalary(salaryAsInteger);
			irsMe.Calculate();

			Intent myIntent = new Intent(MainActivity.this, Result.class);
			MainActivity.this.startActivity(myIntent);

			DecimalFormat formatter = new DecimalFormat("#,###");
			String RemoveCommasTempString = SalaryLabel.getText().toString();
			RemoveCommasTempString = RemoveCommasTempString.replace(",", "");
			SalaryLabel.setText(formatter.format(Integer
					.parseInt(RemoveCommasTempString)));
			// overridePendingTransition(R.anim.fade, R.anim.hold);
			break;
		case R.id.about_button:
			Intent i = new Intent(this, About.class);
			startActivity(i);
			break;
		case R.id.salary_poll:
			Intent salaryPollIntent = new Intent(this, TestMyGoogleAppEngineActivity.class);
			startActivity(salaryPollIntent);
			break;
		default:
			break;

		}
	}

}